#include "stdafx.h"
#include "wf_dimaligned.h"

#include "pf_constructionline.h"



WF_DimAligned::WF_DimAligned(PF_Container& container,
        PF_GraphicView& graphicView)
        :WF_Dimension("Draw aligned dimensions",
                    container, graphicView) {

    reset();
}



WF_DimAligned::~WF_DimAligned() {}

QAction* WF_DimAligned::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	//tr("Aligned")
	QAction* action = new QAction(tr("&Aligned"),  NULL);
	action->setIcon(QIcon(":/extui/dimaligned.png"));
	//action->zetStatusTip(tr("Aligned Dimension"));
	return action;
}


void WF_DimAligned::reset() {
    WF_Dimension::reset();

    edata = PF_DimAlignedAttribute(PF_Vector(false),
                              PF_Vector(false));
    lastStatus = SetExtPoint1;
    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true, true);
    }
}



void WF_DimAligned::trigger() {
    WF_Dimension::trigger();

    preparePreview();
    graphicView->moveRelativeZero(data.definitionPoint);

	//data.text = getText();
    PF_DimAligned* dim =
        new PF_DimAligned(container, data, edata);
    dim->setLayerToActive();
    dim->setPenToActive();
    dim->update();
    container->addEntity(dim);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(dim);
        document->endUndoCycle();
    }

    PF_Vector rz = graphicView->getRelativeZero();
	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(rz);

    PF_DEBUG->print("PF_ActionDimAligned::trigger():"
                    " dim added: %d", dim->getId());
}



void WF_DimAligned::preparePreview() {
    PF_Vector dirV;
    dirV.setPolar(100.0,
                  edata.extensionPoint1.angleTo(
                      edata.extensionPoint2)
                  +M_PI/2.0);
    PF_ConstructionLine cl(NULL,
                           PF_ConstructionLineAttribute(
                               edata.extensionPoint2,
                               edata.extensionPoint2+dirV));

    data.definitionPoint =
        cl.getNearestPointOnEntity(data.definitionPoint);
}



void WF_DimAligned::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("PF_ActionDimAligned::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetExtPoint1:
        break;

    case SetExtPoint2:
        if (edata.extensionPoint1.valid) {
            deletePreview();
            preview->addEntity(
                new PF_Line(preview,
                            PF_LineAttribute(edata.extensionPoint1, mouse))
            );
            drawPreview();
        }
        break;

    case SetDefPoint:
        if (edata.extensionPoint1.valid && edata.extensionPoint2.valid) {
            deletePreview();
            data.definitionPoint = mouse;

            preparePreview();

			//data.text = getText();
            PF_DimAligned* dim = new PF_DimAligned(preview, data, edata);
            dim->update();
            preview->addEntity(dim);
            drawPreview();
        }
        break;

	default:
		break;
    }

    PF_DEBUG->print("PF_ActionDimAligned::mouseMoveEvent end");
}



void WF_DimAligned::mouseReleaseEvent(QMouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DimAligned::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetExtPoint1:
        edata.extensionPoint1 = pos;
        graphicView->moveRelativeZero(pos);
        setStatus(SetExtPoint2);
        break;

    case SetExtPoint2:
        edata.extensionPoint2 = pos;
        graphicView->moveRelativeZero(pos);
        setStatus(SetDefPoint);
        break;

    case SetDefPoint:
        data.definitionPoint = pos;
        trigger();
        reset();
        setStatus(SetExtPoint1);
        break;

    default:
        break;
    }
}



void WF_DimAligned::commandEvent(PF_CommandEvent* e) {
    QString c = e->getCommand().toLower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetText: {
            setText(c);
            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->requestOptions(this, true, true);
            }
            setStatus(lastStatus);
            graphicView->enableCoordinateInput();
        }
        break;

    default:
        if (checkCommand("text", c)) {
            lastStatus = (Status)getStatus();
            graphicView->disableCoordinateInput();
            setStatus(SetText);
        }
        break;
    }
}



QStringList WF_DimAligned::getAvailableCommands() {
    QStringList cmd;

    switch (getStatus()) {
    case SetExtPoint1:
    case SetExtPoint2:
    case SetDefPoint:
        cmd += command("text");
        break;

    default:
        break;
    }

    return cmd;
}



void WF_DimAligned::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetExtPoint1:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify first extension line origin"),
                tr("Cancel"));
            break;
        case SetExtPoint2:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify second extension line origin"),
                tr("Back"));
            break;
        case SetDefPoint:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify dimension line location"),
                tr("Back"));
            break;
        case SetText:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DimAligned::hideOptions() {
    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }

    WF_Dimension::hideOptions();
}



void WF_DimAligned::showOptions() {
    WF_Dimension::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true);
    }
}


// EOF
